#include "CAppStateChangeDif.h"

#include "CAppStateManager.h"

CAppStateChangeDif CAppStateChangeDif::Instance;

CAppStateChangeDif::CAppStateChangeDif() {
    Surf_BG = NULL;
    Surf_Easy = NULL;
    Surf_Medium = NULL;
    Surf_Hard = NULL;
    Surf_Easy_OnLoop = NULL;
    Surf_Medium_OnLoop = NULL;
    Surf_Hard_OnLoop = NULL;

    Level = 0;
}

void CAppStateChangeDif::OnActivate() {
    // Load Simple Logo
    Surf_BG = CSurface::OnLoad("content/image/change_screen.jpg");
    Surf_Easy = CSurface::OnLoad("content/image/Button/easy_pic.jpg");
    Surf_Medium = CSurface::OnLoad("content/image/Button/med_pic.jpg");
    Surf_Hard = CSurface::OnLoad("content/image/Button/hell_pic.jpg");
    Surf_Easy_OnLoop = CSurface::OnLoad("content/image/Button/easy_pic_onloop.jpg");
    Surf_Medium_OnLoop = CSurface::OnLoad("content/image/Button/med_pic_onloop.jpg");
    Surf_Hard_OnLoop = CSurface::OnLoad("content/image/Button/hell_pic_onloop.jpg");

    Font = CFont::OnLoad("content/fonts/visitor2.ttf", 25);
}

void CAppStateChangeDif::OnDeactivate() {
    SDL_FreeSurface(Surf_BG);
    SDL_FreeSurface(Surf_Easy);
    SDL_FreeSurface(Surf_Medium);
    SDL_FreeSurface(Surf_Hard);
}

void CAppStateChangeDif::OnLoop() {

}

void CAppStateChangeDif::OnRender(SDL_Surface* Surf_Display) {
    CSurface::OnDraw(Surf_Display, Surf_BG, 0, 0);
    if (Level == 0)
        CSurface::OnDraw(Surf_Display, Surf_Easy_OnLoop, 42, 202);
    else
        CSurface::OnDraw(Surf_Display, Surf_Easy, 42, 202);

    if (Level == 1)
        CSurface::OnDraw(Surf_Display, Surf_Medium_OnLoop, 242, 202);
    else
        CSurface::OnDraw(Surf_Display, Surf_Medium, 242, 202);

    if (Level == 2)
        CSurface::OnDraw(Surf_Display, Surf_Hard_OnLoop, 442, 202);
    else
        CSurface::OnDraw(Surf_Display, Surf_Hard, 442, 202);
}

CAppStateChangeDif* CAppStateChangeDif::GetInstance() {
    return &Instance;
}

void CAppStateChangeDif::OnKeyDown(SDLKey sym, SDLMod mod, Uint16 unicode) {
    switch (sym) {
        case SDLK_RIGHT:
            if (Level + 1 < 3)
                Level++;
        break;
        case SDLK_LEFT:
            if (Level - 1 >= 0)
                Level--;
        break;
        case SDLK_RETURN: {
            CScore::Score.Level = Level;
            CField::Field.Live = false;
            CAppStateManager::SetActiveAppState(APPSTATE_GAME);
        }
        break;
        case SDLK_ESCAPE:
            CAppStateManager::SetActiveAppState(APPSTATE_MENU);
        break;
    }
}

void CAppStateChangeDif::OnMouseMove(int mX, int mY, int relX, int relY, bool Left,bool Right,bool Middle) {
    if (mX >= 42 && mX <= 42 + Surf_Easy->w && mY >= 202 && mY <= 202 + Surf_Easy->h)
        Level = 0;
    if (mX >= 242 && mX <= 242 + Surf_Medium->w && mY >= 202 && mY <= 202 + Surf_Medium->h)
        Level = 1;
    if (mX >= 442 && mX <= 442 + Surf_Hard->w && mY >= 202 && mY <= 202 + Surf_Hard->h)
        Level = 2;
}

void CAppStateChangeDif::OnLButtonDown(int mX, int mY) {
    if ((mX >= 42 && mX <= 42 + Surf_Easy->w && mY >= 202 && mY <= 202 + Surf_Easy->h) ||
    (mX >= 242 && mX <= 242 + Surf_Medium->w && mY >= 202 && mY <= 202 + Surf_Medium->h) ||
    (mX >= 442 && mX <= 442 + Surf_Hard->w && mY >= 202 && mY <= 202 + Surf_Hard->h)) {
        CScore::Score.Level = Level;
        CField::Field.Live = false;
        CAppStateManager::SetActiveAppState(APPSTATE_GAME);
    }
}
